package com.example.administrator.dialog;

import android.app.DatePickerDialog;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.DateTimeKeyListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;
import java.util.Calendar;
import java.util.Locale;

public class DialogDatePicker extends AppCompatActivity {
    private TextView showdate=null;
    private TextView text=null;
    private Button setdate=null;
    private int year;
    private int month;
    private int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_date_picker);

        showdate = (TextView)findViewById(R.id.showtime);
        setdate = (Button)findViewById(R.id.setdate);
        text = (TextView)findViewById(R.id.testtv);

        Calendar c = Calendar.getInstance(Locale.CHINA);
        Date mydate = new Date();
        c.setTime(mydate);

        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        showdate.setText("当前日期："+year+"-"+(month+1)+"-"+day);

        setdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog my_datePickDialog = new DatePickerDialog(DialogDatePicker.this,Datelistener,year,month,day);
                my_datePickDialog.show();
            }
        });
    }
    private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int y, int m, int d) {

            Date date=new Date(year-1900,month,day);
            Date currentTime = new Date();

                year = y;
                month = m;
                day = d;

                //update time
                updateDate();


//            if(date.getTime()<currentTime.getTime()) {
//                year = y;
//                month = m;
//                day = d;
//
//                //update time
//                updateDate();
//            }
//            else {
//                Toast.makeText(DialogDatePicker.this,"Wrong time!",Toast.LENGTH_SHORT).show();
//            }
        }

        private void updateDate(){
            showdate.setText("当前日期："+year+"-"+(month+1)+"-"+day);
        }
    };
}
